/*
 * ServerGui.java
 *
 * Created on June 5, 2008, 8:38 AM
 */

package server;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
/**
 *
 * @author  ew562c
 */
public class ServerGui extends javax.swing.JFrame { //implements Runnable {
    
    private final ClientNode clients[];
    private DefaultListModel userListModel;
    /** Creates new form ServerGui */
    public ServerGui(String serverName, ClientNode[] clients, String port) {
        this.clients = clients;
        initComponents();
        sendMessageBtn.setEnabled(false);
        kickUserBtn.setEnabled(false);
        userListModel = new DefaultListModel();
        userListModel.clear();
        userList.setModel(userListModel);
        usersLbl.setText("Users (0/"+clients.length+"):");
        this.setTitle(serverName);
        portLbl.setText(port);
        updateGui(0,true);
    }
  
    public void updateGui(int clientUpdated, boolean force)
    {
        int count_valid = 0;
        int selected    = userList.getSelectedIndex();
        String selected_name = (String)userList.getSelectedValue();
        int sClientId  = -1;
        
        if(userSlotLbl.getText().compareTo("") != 0)
        {
            sClientId = Integer.valueOf(userSlotLbl.getText()).intValue();
        }
        
        if(clientUpdated == sClientId || force)
        {
            userListModel.clear();
            for(int i = 0; i < clients.length; i++)
            {
                if(clients[i].valid)
                {
                    
                    if(clients[i].name != null)
                        userListModel.add(count_valid,clients[i].name);
                    else
                        userListModel.add(count_valid,"?");
                    
                    count_valid++;
                    usersLbl.setText("Users ("+count_valid+"/"+clients.length+"):");
                }
            }
            usersLbl.setText("Users ("+count_valid+"/"+clients.length+"):");
        }
        
        userList.setSelectedIndex(selected);       
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        userList = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        sendMessageBtn = new javax.swing.JButton();
        kickUserBtn = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        userNameLbl = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        timeConnectedLbl = new javax.swing.JLabel();
        idleTimeLbl = new javax.swing.JLabel();
        positionLbl = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        userSlotLbl = new javax.swing.JLabel();
        usersLbl = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        shutdownBtn = new javax.swing.JButton();
        broadcastBtn = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        portLbl = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        userList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                userListValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(userList);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        sendMessageBtn.setText("Send Msg");
        sendMessageBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendMessageBtnActionPerformed(evt);
            }
        });

        kickUserBtn.setText("Kick User");
        kickUserBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kickUserBtnActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("User Name:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Time Connected:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setText("Idle Time:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel6.setText("Position:");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("User Slot:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(sendMessageBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(16, 16, 16)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(userNameLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(userSlotLbl, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                                        .addComponent(timeConnectedLbl, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(idleTimeLbl, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))
                                    .addComponent(positionLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(35, 35, 35))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(49, 49, 49)
                                .addComponent(kickUserBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(userNameLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(userSlotLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(timeConnectedLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(idleTimeLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(positionLbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kickUserBtn)
                    .addComponent(sendMessageBtn))
                .addContainerGap())
        );

        usersLbl.setFont(new java.awt.Font("Tahoma", 1, 14));
        usersLbl.setText("Users(0/0):");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel2.setText("User Info:");

        shutdownBtn.setText("Shutdown Server");
        shutdownBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                shutdownBtnActionPerformed(evt);
            }
        });

        broadcastBtn.setText("Broadcast Message");
        broadcastBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                broadcastBtnActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel7.setText("Server Info:");

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel10.setText("Port:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(portLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
            .addComponent(portLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(usersLbl)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(broadcastBtn))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7)
                        .addContainerGap(216, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2)
                            .addComponent(shutdownBtn, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usersLbl)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(broadcastBtn)
                    .addComponent(shutdownBtn))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void userListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_userListValueChanged
// TODO add your handling code here:
        String selUser = ((String)userList.getSelectedValue());
        sendMessageBtn.setEnabled(false);
        kickUserBtn.setEnabled(false);
        userNameLbl.setText("");
        positionLbl.setText("");
        timeConnectedLbl.setText("");
        idleTimeLbl.setText("");
        userSlotLbl.setText("");
        
        for(int i = 0; i < clients.length; i++)
        {
            if(clients[i].valid && clients[i].name != null && selUser != null)
            {
                if(selUser.compareTo(clients[i].name)==0)
                {
                    userNameLbl.setText(clients[i].name);
                    positionLbl.setText(clients[i].x_pos +","+clients[i].y_pos+","+clients[i].z_pos);
                    timeConnectedLbl.setText(String.valueOf(clients[i].connect_time)); 
                    idleTimeLbl.setText(String.valueOf(clients[i].idle_time)); 
                    userSlotLbl.setText(String.valueOf(i));
                    sendMessageBtn.setEnabled(true);
                    kickUserBtn.setEnabled(true);
                    break;
                }
            }
        }       
    }//GEN-LAST:event_userListValueChanged

    private void shutdownBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shutdownBtnActionPerformed
// TODO add your handling code here:
        System.exit(1);
    }//GEN-LAST:event_shutdownBtnActionPerformed

    private void broadcastBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_broadcastBtnActionPerformed
// TODO add your handling code here:
        String response = JOptionPane.showInputDialog(null,
              "Send Broadcast Message",
              "Enter Message",
              JOptionPane.QUESTION_MESSAGE);
        if(response != null)
        {
            for(int i = 0; i < clients.length; i++)
            {
                if(clients[i].valid)
                {
                    clients[i].msg_in_box += "[SERVER>";
                    clients[i].msg_in_box += response;
                    clients[i].msg_in_box += "]";    
                }
            }   
        }
    }//GEN-LAST:event_broadcastBtnActionPerformed

    private void kickUserBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kickUserBtnActionPerformed
// TODO add your handling code here:
        if(userSlotLbl.getText().compareTo("") != 0)
        {
            clients[Integer.valueOf(userSlotLbl.getText()).intValue()].valid = false;
        }
            
    }//GEN-LAST:event_kickUserBtnActionPerformed

    private void sendMessageBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendMessageBtnActionPerformed
// TODO add your handling code here:
        String response = JOptionPane.showInputDialog(null,
              "Send Message to "+userNameLbl.getText(),
              "Enter Message",
              JOptionPane.QUESTION_MESSAGE);
        int index = Integer.valueOf(userSlotLbl.getText()).intValue();
        if(response != null && index >= 0)
        {
            clients[index].msg_in_box += "[SERVER>";
            clients[index].msg_in_box += response;
            clients[index].msg_in_box += "]";    
        }
    }//GEN-LAST:event_sendMessageBtnActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton broadcastBtn;
    private javax.swing.JLabel idleTimeLbl;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton kickUserBtn;
    private javax.swing.JLabel portLbl;
    private javax.swing.JLabel positionLbl;
    private javax.swing.JButton sendMessageBtn;
    private javax.swing.JButton shutdownBtn;
    private javax.swing.JLabel timeConnectedLbl;
    private javax.swing.JList userList;
    private javax.swing.JLabel userNameLbl;
    private javax.swing.JLabel userSlotLbl;
    private javax.swing.JLabel usersLbl;
    // End of variables declaration//GEN-END:variables
    
}
